﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public static partial class user
    {
        public static dynamic doReg(string username, string password, string nickname, string email, string motto,
            string sid, string name, string school, bool? sex, DateTime? birthday, bool publicPersonalInfo)
        {
            var sql = new Sql.SqlDataContext();
            if (sql.User.Any(row => row.username.ToLower() == username.ToLower())) return new
                  {
                      success = false,
                      msg = "用户名已存在"
                  };
            var u = new Sql.User
            {
                username = username,
                password = password,
                nickname = nickname,
                email = email,
                motto = motto,
                publicPersonalInfo = publicPersonalInfo,
                sid = sid,
                name = name,
                sex = sex,
                school = school,
                birthday = birthday,
                admin = false,
                regTime = DateTime.Now,
                lastLoginTime = DateTime.Now,
                disabled = false
            };
            if (sid != null) // Override student info
            {
                var s = sql.Student.FirstOrDefault(row => row.sid == sid);
                if (s != null)
                {
                    u.name = s.Name;
                    u.sex = s.Sex;
                    u.school = "合肥工业大学";
                    u.birthday = s.Birthday;
                }
            }
            sql.User.InsertOnSubmit(u);

            var us = new Sql.UserStatistics { username = username };
            us.accept = us.ce = us.mle = us.ole = us.pe = us.re = us.se = us.submit = us.tle = us.wa = 0;
            sql.UserStatistics.InsertOnSubmit(us);

            sql.SubmitChanges();
            return new
            {
                success = true
            };
        }
    }
}
